package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.SysUser;
import com.cz.zslu.entity.WithdrawBill;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.service.ISysUserService;
import com.cz.zslu.service.IWithdrawBillService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 提现申请 前端控制器
 * </p>
 *
 * @author 森森
 * @since 2018-11-28
 */
@RestController
@RequestMapping("/withdrawBill")
public class WithdrawBillController {
    @Autowired
    private IWithdrawBillService iWithdrawBillService;
    @Autowired
    private ISysUserService iSysUserService;


    @ApiOperation("申请提现")
    @RequestMapping(value = "/applyWithdraw", method = RequestMethod.POST)
    public ReturnObject<WithdrawBill> applyWithdraw(@ApiParam("提现金额") BigDecimal amount,
                                                    @ApiParam("银行名称") String bankName,
                                                    @ApiParam("银行卡号") String bankCard,
                                                    @ApiParam("开卡人姓名") String openedPeople,
                                                    @ApiParam("开户行") String bankDeposit) {
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        if (sysUser.getCanCarry().doubleValue() >= amount.doubleValue()) {
            WithdrawBill withdrawBill = new WithdrawBill();
            withdrawBill.setAmount(amount);
            withdrawBill.setBankName(bankName);
            withdrawBill.setBankCard(bankCard);
            withdrawBill.setOpenedPeople(openedPeople);
            withdrawBill.setBankDeposit(bankDeposit);
            withdrawBill.setPhone(sysUser.getPhone());
            withdrawBill.setStoreName(sysUser.getStoreName());
            withdrawBill.setAudit(0);
            iWithdrawBillService.insert(withdrawBill);
            sysUser.setCanCarry(sysUser.getCanCarry().subtract(amount));
            sysUser.setWithdrawalOf(sysUser.getWithdrawalOf().add(amount));
            iSysUserService.updateById(sysUser);
            return new ReturnObject<>(0, "提现成功", withdrawBill);
        }
        return new ReturnObject<>(1, "提现失败，余额不足");
    }


    @ApiOperation("后台获取全部提现申请")
    @RequestMapping(value = "/getAllPage", method = RequestMethod.POST)
    public ReturnObject<List<WithdrawBill>> getAllPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                       @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                       @ApiParam("提现类型（0:申请中，1：打款成功，2：打款失败）") Integer audit) {

        EntityWrapper<WithdrawBill> ew = new EntityWrapper<>();
        if (audit != null) {
            ew.eq("audit", audit);
        }
        ew.orderBy("crt_time", false);
        Page<WithdrawBill> obj = iWithdrawBillService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("用户获取提现申请记录")
    @RequestMapping(value = "/getyMAllPage", method = RequestMethod.POST)
    public ReturnObject<List<WithdrawBill>> getyMAllPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                         @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                         @ApiParam("提现类型（0:申请中，1：打款成功，2：打款失败）") Integer audit) {
        EntityWrapper<WithdrawBill> ew = new EntityWrapper<>();
        if (audit != null) {
            ew.eq("audit", audit);
        }
        ew.eq("user_id", BaseContextHandler.getUserID());
        ew.orderBy("crt_time", false);
        Page<WithdrawBill> obj = iWithdrawBillService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("提现成功")
    @RequestMapping(value = "/withdrawalSuccess", method = RequestMethod.POST)
    public ReturnObject withdrawalSuccess(@ApiParam("订单id") Long id) {
        WithdrawBill info = iWithdrawBillService.selectById(id);
        SysUser sysUser = iSysUserService.selectById(info.getUserId());
        info.setAudit(1);
        iWithdrawBillService.updateById(info);
        sysUser.setWithdrawalOf(sysUser.getWithdrawalOf().subtract(info.getAmount()));
        sysUser.setHaveWithdrawal(sysUser.getHaveWithdrawal().add(info.getAmount()));
        iSysUserService.updateById(sysUser);
        return new ReturnObject<>(0, "打款成功");
    }

    @ApiOperation("提现失败")
    @RequestMapping(value = "/withdrawalFailure", method = RequestMethod.POST)
    public ReturnObject withdrawalFailure(@ApiParam("订单id") Long id) {

        WithdrawBill info = iWithdrawBillService.selectById(id);
        SysUser sysUser = iSysUserService.selectById(info.getUserId());
        info.setAudit(3);
        iWithdrawBillService.updateById(info);
        sysUser.setWithdrawalOf(sysUser.getWithdrawalOf().subtract(info.getAmount()));
        sysUser.setCanCarry(sysUser.getCanCarry().add(info.getAmount()));
        iSysUserService.updateById(sysUser);
        return new ReturnObject<>(0, "打款失败");
    }

}

